home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / dte5_1.zip / HWIBM.C < prev    next >
C/C++ Source or Header  |  1991-02-06  |  30KB  |  988 lines

  1. /*
  2.  * Written by Douglas Thomson (1989/1990)
  3.  *
  4.  * This source code is released into the public domain.
  5.  */
  6.  
  7. /*
  8.  * Name:    dte - Doug's Text Editor program - hardware dependent module
  9.  * Purpose: This file contains all the code that needs to be different on
  10.  *           different hardware.
  11.  * File:    hwibm.c
  12.  * Author:  Douglas Thomson
  13.  * System:  This particular version is for the IBM PC and close compatibles.
  14.  *           It write directly to video RAM, so it is faster than other
  15.  *           techniques, but will cause "snow" on most CGA cards. See the
  16.  *           file "hwibmcga.c" for a version that avoids snow.
  17.  *          The compiler is Turbo C 2.0, using one of the large data memory
  18.  *           models.
  19.  * Date:    October 10, 1989
  20.  * Notes:   This module has been kept as small as possible, to facilitate
  21.  *           porting between different systems.
  22.  */
  23. #include "common.h"     /* dte types */
  24. #include "hwdep.h"      /* prototypes for functions here */
  25. #include "utils.h"      /* for displaying messages etc */
  26. #include "version.h"    /* current version number */
  27. #include <stdarg.h>     /* for passing variable numbers of arguments */
  28. #include <conio.h>      /* for using putch to output a character */
  29. #include <dos.h>        /* for renaming files */
  30. #include <dir.h>        /* for searching the current path */
  31. #include <bios.h>       /* for direct BIOS keyboard input */
  32. #include <alloc.h>      /* for memory allocation */
  33. #include <io.h>         /* for file attribute code */
  34. #include <fcntl.h>      /* open flags */
  35. #include <process.h>    /* spawn etc */
  36. #include <sys/stat.h>   /* S_IWRITE etc */
  37.  
  38. /*
  39.  * prototypes for all functions in this file
  40.  */
  41. void error ARGS((int kind, ...));
  42. void main ARGS((int argc, char *argv[]));
  43. void hw_xygoto ARGS((void));
  44. int hw_clreol ARGS((void));
  45. int hw_linedel ARGS((int line));
  46. int hw_scroll_up ARGS((int top, int bottom));
  47. int hw_lineins ARGS((int line));
  48. int hw_scroll_down ARGS((int top, int bottom));
  49. int hw_c_avail ARGS((void));
  50. int hw_c_input ARGS((void));
  51. void hw_c_output ARGS((int c));
  52. void hw_terminate ARGS((void));
  53. void hw_initialize ARGS((void));
  54. void hw_move ARGS((text_ptr dest, text_ptr source, long number));
  55. int hw_backspace ARGS((void));
  56. int hw_c_insert ARGS((void));
  57. int hw_c_delete ARGS((void));
  58. int hw_rename ARGS((char *old, char *new));
  59. int hw_fattrib ARGS((char *name));
  60. int hw_set_fattrib ARGS((char *name, int attrib));
  61. int hw_unlink ARGS((char *name));
  62. int hw_printable ARGS((int c));
  63. static int write_file ARGS((char *name, char *mode, text_ptr start,
  64.         text_ptr end));
  65. int hw_save ARGS((char *name, text_ptr start, text_ptr end));
  66. int hw_append ARGS((char *name, text_ptr start, text_ptr end));
  67. int hw_print ARGS((text_ptr start, text_ptr end));
  68. int hw_load ARGS((char *name, text_ptr start, text_ptr limit, text_ptr *end));
  69. void hw_copy_path ARGS((char *old, char *name, char *new));
  70. int hw_os_shell ARGS((void));
  71.  
  72. /*
  73.  * Name:    error
  74.  * Purpose: To report an error, and usually make the user type <ESC> before
  75.  *           continuing.
  76.  * Date:    October 10, 1989
  77.  * Passed:  kind:   an indication of how serious the error was:
  78.  *                      TEMP:    merely a message, do not wait for <ESC>
  79.  *                      DIAG:    merely a message, but make sure user sees it
  80.  *                      WARNING: error, but editor can continue after <ESC>
  81.  *                      FATAL:   abort the editor!
  82.  *          format: printf format string for any arguments that follow
  83.  *          ...:    arguments to be printed
  84.  * Notes:   This function should be system independent; that is the whole
  85.  *           point of the "stdarg" philosophy. However, two of the systems
  86.  *           I have used implemented "stdarg" incompatibly, and some older
  87.  *           systems may not support the "stdarg" macros at all...
  88.  */
  89. void error(kind, format)
  90. int kind;
  91. char *format;
  92. {
  93.     va_list argptr;         /* used to access various arguments */
  94.     char buff[MAX_COLS];    /* somewhere to store error before printing */
  95.     int c;                  /* character entered by user to continue */
  96.  
  97.     /*
  98.      * prepare to process variable arguments
  99.      */
  100.     va_start(argptr, format);
  101.  
  102.     /*
  103.      * tell the user what kind of an error it is
  104.      */
  105.     switch (kind) {
  106.     case FATAL:
  107.         strcpy(buff, "Fatal error: ");
  108.         break;
  109.     case WARNING:
  110.         strcpy(buff, "Warning: ");
  111.         break;
  112.     case DIAG:
  113.     case TEMP:
  114.         strcpy(buff, "");
  115.         break;
  116.     }
  117.  
  118.     /*
  119.      * prepare the error message itself
  120.      */
  121.     vsprintf(buff + strlen(buff), format, argptr);
  122.     va_end(argptr);
  123.  
  124.     /*
  125.      * tell the user how to continue editing if necessary
  126.      */
  127.     if (kind == WARNING || kind == DIAG) {
  128.         strcat(buff, ": type <ESC>");
  129.     }
  130.  
  131.     /*
  132.      * output the error message
  133.      */
  134.     set_prompt(buff, 1);
  135.  
  136.     if (kind == FATAL) {
  137.         /*
  138.          * no point in making the user type <ESC>, since the program is
  139.          *  about to abort anyway...
  140.          */
  141.         terminate();
  142.         exit(1);
  143.     }
  144.     else if (kind != TEMP) {
  145.         /*
  146.          * If necessary, force the user to acknowledge the error by
  147.          *  typing <ESC> (or ^U).
  148.          * This prevents any extra commands the user has entered from
  149.          *  causing problems after an error may have made them inappropriate.
  150.          */
  151.         while ((c=c_input()) != 27 && c != CONTROL('U')) {
  152.             set_prompt(buff, 1);
  153.         }
  154.     }
  155. }
  156.  
  157. /*
  158.  * Name:    harmless
  159.  * Purpose: To process control-break by ignoring it, so that the editor is
  160.  *           not aborted!
  161.  * Date:    February 5, 1990
  162.  */
  163. static int harmless(void)
  164. {
  165.     return 1;   /* ignore */
  166. }
  167.  
  168.  
  169. /*
  170.  * original control-break checking flag
  171.  */
  172. static int s_cbrk;
  173.  
  174. /*
  175.  * Name:    main
  176.  * Purpose: To do any system dependent command line argument processing,
  177.  *           and then call the main editor function.
  178.  * Date:    October 10, 1989
  179.  * Passed:  argc:   number of command line arguments
  180.  *          argv:   text of command line arguments
  181.  */
  182. void main(argc, argv)
  183. int argc;
  184. char *argv[];
  185. {
  186.     char drive[MAXDRIVE];  /* drive which dte.exe came from */
  187.     char dir[MAXDIR];      /* directory for dte.exe */
  188.  
  189.     /*
  190.      * trap control-break to make it harmless, and turn checking off
  191.      */
  192.     s_cbrk = getcbrk();
  193.     ctrlbrk(harmless);
  194.     setcbrk(0);
  195.  
  196.     /*
  197.      * set up help file name. This is a file called dte.hlp, and it should
  198.      *  be in the same directory as the dte.exe program.
  199.      * This information is only available in DOS 3 and later, so we need
  200.      *  to check to see whether argv[0] was OK.
  201.      */
  202.     if (fnsplit(argv[0], drive, dir, NULL, NULL) & DIRECTORY) {
  203. #ifdef GRIB
  204.         fnmerge(g_status.help_file, drive, dir, "dtegrib", ".hlp");
  205. #else
  206.         fnmerge(g_status.help_file, drive, dir, "dte", ".hlp");
  207. #endif
  208.     }
  209.     editor(argc, argv);
  210. }
  211.  
  212. /*
  213.  * The following defines specify which video attributes give desired
  214.  *  effects on different display devices.
  215.  * REVERSE is supposed to be reverse video - a different background color,
  216.  *  so that even a blank space can be identified.
  217.  * HIGH is supposed to quickly draw the user's eye to the relevant part of
  218.  *  the screen, either for a message or for matched text in find/replace.
  219.  * NORMAL is supposed to be something pleasant to look at for the main
  220.  *  body of the text.
  221.  * These defines may not be optimal for all types of display. Eventually
  222.  *  the user should be allowed to select which attribute is used where.
  223.  */
  224. #define LCD_REVERSE 0x70
  225. #define LCD_NORMAL  0x07
  226. #define LCD_HIGH    0x17
  227.  
  228. #define HERC_REVERSE 0x70
  229. #define HERC_UNDER   0x01
  230. #define HERC_NORMAL  0x07
  231. #define HERC_HIGH    0x0F
  232.  
  233. #define COLOR_NORMAL 0x07
  234. #define COLOR_REVERSE 0x17
  235. #define COLOR_HIGH 0x1F
  236.  
  237. /*
  238.  * Name:    hw_xygoto
  239.  * Purpose: To move the cursor to a new position on the screen.
  240.  * Date:    October 10, 1989
  241.  * Passed:  [g_display.line]: the required line
  242.  *          [g_display.col]:  th